package com.liqin.nothing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liqin.nothing.entity.Person;
import com.liqin.nothing.mapper.PersonMapper;
import com.liqin.nothing.service.PersonService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gonghl
 * @since 2024-05-28
 */
@Service
@RequiredArgsConstructor
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {

    private final PersonMapper personMapper;

    @Override
    public List<Person> getPerson() {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .gt(Person::getAge, 1)
                .nested(b -> b.eq(Person::getSex, "男").or().eq(Person::getSex, "女"))
                .nested(b -> b.eq(Person::getCardId, 1).or().isNull(Person::getCardId))
                .list();
    }

    // @Override
    // public List<Person> getPerson() {
    //     return new LambdaQueryChainWrapper<>(baseMapper)
    //             .inSql(Person::getSex, "select '男'")
    //             .list();
    // }

    @Override
    public List<Object> getPersonName() {
        LambdaQueryWrapper<Person> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Person::getName);
        queryWrapper.groupBy(Person::getName);
        return baseMapper.selectObjs(queryWrapper);
    }

    @Override
    public Page<Person> page(Page<Person> page, Person person) {
        // if (true) {
        //     throw new TestException("我的自定义异常类");
        // }
        // return page(page, new QueryWrapper<>(person));
        return personMapper.pagePerson(page, person);
    }

}
